package org.aeon.newsmanagersystem.controller;

import org.aeon.newsmanagersystem.pojo.Comments;
import org.aeon.newsmanagersystem.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * @author: MissingAeon
 * @date: 2024/3/27 17:28
 * @description:
 */
@Controller
@RequestMapping("/comments")
public class CommentsController {
    private final CommentsService commentsService;

    @Autowired
    public CommentsController(CommentsService commentsService) {
        this.commentsService = commentsService;
    }

    @GetMapping("/delete")
    public String delete(Integer cid, @RequestHeader("Referer") String referer, Model model) {
        final boolean result = commentsService.delete(cid);
        if (result) {
            model.addAttribute("result", "成功删除!");
        } else {
            model.addAttribute("result", "删除失败!");
        }
        return "redirect:" + referer;
    }

    @PostMapping("/add")
    public String add(Comments comments, @RequestHeader("Referer") String referer, Model model) {
        comments.setCdate(new Date());
        final boolean result = commentsService.add(comments);
        if (result) {
            model.addAttribute("result", "成功评论!");
        } else {
            model.addAttribute("result", "评论失败!");
        }
        return "redirect:" + referer;
    }
}
